/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package backingbeans;

import entities.Music;
import entities.Playlist;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import lazydatamodels.LazyMusicDataModel;
import org.primefaces.model.LazyDataModel;
import sessions.MusicManager;
import sessions.PlaylistManager;
import sessions.UserManager;

/**
 *
 * @author Antoine Boulinguez et Shyn-Yuan Cheng
 */
@ManagedBean(name="songList")
@ViewScoped
public class SongList implements Serializable {
    @EJB
    private UserManager userManager;
    @EJB
    private PlaylistManager playlistManager;
    @EJB
    private MusicManager musicManager;
    private LazyDataModel<Music> lazyMusicModel = new LazyMusicDataModel();  
    private Music song;
    private Playlist playlist;
    
    @Inject
    private UserSession userSession;

    public LazyDataModel<Music> getLazyMusicModel() {
        return lazyMusicModel;
    }

    public void setLazyMusicModel(LazyDataModel<Music> lazyMusicModel) {
        this.lazyMusicModel = lazyMusicModel;
    }

    public Music getSong() {
        return song;
    }

    public void setSong(Music song) {
        this.song = song;
    }

    public Playlist getPlaylist() {
        return playlist;
    }

    public void setPlaylist(Playlist playlist) {
        this.playlist = playlist;
    }
    
    public List<Music> getSongs() {
        return musicManager.getAllMusics();
    }
    
    // Escape slashes in a string with backslashes
    public String addSlashes(String word) {
        return word.replaceAll("'", "\\\\'");
    }
    
    public String escapeSlashes(String word) {
        return word.replaceAll("\\\\", "\\\\\\\\");
    }
    
    public void addToPlayList() {
        playlist.addSong(song);
        playlist.setTotalTime(playlist.getTotalTime() + song.getTotalTime());
        playlistManager.update(playlist);
        userSession.setUser(userManager.getUser(userSession.getUser().getId()));
    }
}
